package com.mine.test.graphql;

import com.mine.test.model.User;
import graphql.GraphQL;
import graphql.annotations.GraphQLAnnotations;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Project Name is testql.
 * User: wwq
 * Date: 2017/6/2
 * Time: 下午1:13
 */
@Component
public class GraphSchema {
    @Autowired
    private RootQuery query;

    private GraphQL graphQL;

    public GraphSchema() throws IllegalAccessException, NoSuchMethodException, InstantiationException {
        this.graphQL = createGraphQLSchema();
    }
    public Object execute(String queryString) {
        Object returnData;
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("data", graphQL.execute(queryString).getData());
        returnData = result;
        return returnData;

    }

    private GraphQL createGraphQLSchema() throws IllegalAccessException, InstantiationException, NoSuchMethodException {
        GraphQLObjectType queryObject = GraphQLAnnotations.object(RootQuery.class);

        return new GraphQL(GraphQLSchema.newSchema().query(queryObject).build());
    }
}
